{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*- "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys,os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "current_path= os.getcwd()\n",
    "#current_dir=os.path.dirname(current_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_path=os.path.join(current_path,'dataset')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.append(dataset_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "###将文件读出到列表，表格的每个元素由一个字典组成\n",
    "def read_dat(fn):\n",
    "    f=open(fn,mode='r', encoding='UTF-8')\n",
    "    lines=[l for l in f]\n",
    "\n",
    "    res=[]\n",
    "    for i in range(len(lines)):\n",
    "        if i==0:\n",
    "            keys=lines[i].strip('\\n').split('\\t')\n",
    "        else:\n",
    "            temp=lines[i].strip('\\n').split('\\t')\n",
    "            rec=dict()\n",
    "            for j in range(len(keys)):\n",
    "\n",
    "                rec[keys[j]]=temp[j]\n",
    "            res.append(rec)\n",
    "    return keys,res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#read_dat(fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import tanh\n",
    "import sqlite3 as sqlite3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "##保存入sqlite数据库\n",
    "\n",
    "con = sqlite3.connect('dataset.db')\n",
    "\n",
    "data_files=os.listdir(dataset_path)\n",
    "\n",
    "for data_file in data_files:\n",
    "    fn=os.path.join(dataset_path,data_file)\n",
    "    #print(\"fn:\",fn)\n",
    "    try:\n",
    "        keys,res=read_dat(fn)\n",
    "\n",
    "        table_name=data_file.rstrip('.dat')\n",
    "        #print(\"table_name:\",table_name)\n",
    "\n",
    "        #create table\n",
    "        x=' varchar(512),'.join(keys)+' varchar(512)'\n",
    "        #print(x)\n",
    "        con.execute(\"create table %s\"%table_name+\" (%s)\"%x)\n",
    "        #print('create table %s'%table_name,' success')\n",
    "\n",
    "        #insert values into table\n",
    "        #insert values into table\n",
    "        for i in range(len(res)):\n",
    "            #限定数据集大小，python sqlite比较慢？\n",
    "            if i <200:\n",
    "                y=res[i]\n",
    "                #print('y: ',y)\n",
    "                z='\\''+'\\',\\''.join(list(y.values()))+'\\''\n",
    "                con.execute(\"INSERT INTO %s\"%table_name+\" VALUES(%s)\"%z)\n",
    "                con.commit()\n",
    "            else:\n",
    "                continue\n",
    "    except:\n",
    "        continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
